package com.woniu.cms.psm.core.controller;


import com.woniu.cms.psm.core.entity.PmPlan;
import com.woniu.cms.psm.core.service.IPmPlanService;
import com.woniu.cms.psm.core.vo.PlanVo;
import com.woniu.common.api.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lilili
 * @since 2022-09-16
 */
@RestController
@RequestMapping("/pm-plan")
@Api(tags = "项目进度计划控制器")
public class PmPlanController {
    @Resource
    private IPmPlanService planService;

    @ApiOperation("获取所有进度信息")
    @GetMapping("/all")
    public Result<List<PlanVo>> all(){
        List<PmPlan> list = planService.list();
        //映射时算出超时时间
        List<PlanVo> collect = list.stream().map(e -> {
            PlanVo planVo = new PlanVo();
            BeanUtils.copyProperties(e, planVo);
            int timeOut = (int) ((new Date().getTime() - planVo.getPlanEndDate().getTime())/86400000);
            planVo.setTimeout("超时"+timeOut+"天");
            return planVo;
        }).collect(Collectors.toList());

        return Result.common(collect);
    }

    /**
     * 防止前端点错。id，实际开始结束时间，实际进度均置为null
     *
     * @param plan 计划
     * @return {@link Result}<{@link Object}>
     */
    @ApiOperation("增加项目进度信息")
    @PostMapping("/add")
    public Result<Object> add(@RequestBody PlanVo plan){
        plan.setId(null);
        plan.setPracticalBeginDate(null);
        plan.setPracticalEndDate(null);
        plan.setParcticalPercent(null);
        PmPlan pmPlan = new PmPlan();
        BeanUtils.copyProperties(plan, pmPlan);
        boolean save = planService.save(pmPlan);
        return Result.common(save);
    }

    @ApiOperation("实际进度维护")
    @PutMapping("/update")
    public Result<Object> update(@RequestBody PlanVo plan){
        PmPlan pmPlan = new PmPlan();
        BeanUtils.copyProperties(plan, pmPlan);
        boolean update = planService.updateById(pmPlan);
        return Result.common(update);
    }

    @ApiOperation("批量删除项目进度信息")
    @PutMapping("/delete")
    public Result<Object> update(@RequestBody Integer[] ids){
        boolean delete = planService.removeBatchByIds(Arrays.asList(ids));
        return Result.common(delete);
    }

}

